package com.it.web.service.impl;

import com.it.common.domain.dos.ArticleDO;
import com.it.common.mapper.ArticleMapper;
import com.it.common.mapper.CategoryMapper;
import com.it.common.mapper.TagMapper;
import com.it.common.utils.Response;
import com.it.web.model.vo.statistics.FindStatisticsInfoRspVO;
import com.it.web.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private TagMapper tagMapper;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     *获取文章总数、分类总数、标签总数、总访问量统计信息
     *
     * @return {@link Response }
     */
    @Override
    public Response findInfo() {
        //获取文章总数
        Long articleTotalCount = articleMapper.selectArticleTotal();

        //获取标签总数
        Long tagTotalCount = tagMapper.selectTagTotal();

        //获取分类总数
        Long categoryTotalCount = categoryMapper.selectCategoryTotal();

        //获取总浏览量
        List<ArticleDO> articleDOS = articleMapper.selectAllReadNum();
        Long pvTotalCount = 0L;

        if(!CollectionUtils.isEmpty(articleDOS)){
            for(ArticleDO articleDO:articleDOS){
                pvTotalCount += articleDO.getReadNum();
            }
        }
        // 组装 VO 类
        FindStatisticsInfoRspVO vo = FindStatisticsInfoRspVO.builder()
                .articleTotalCount(articleTotalCount)
                .categoryTotalCount(categoryTotalCount)
                .tagTotalCount(tagTotalCount)
                .pvTotalCount(pvTotalCount).build();
        return Response.success(vo);
    }

}
